<?php
if (!defined("_INDEX")) { header("Location: /"); die; }

$topic_id = $route->getByNum(5);

$topic = $db->getRow("select `T`.*,`R`.`user_id` from `topics` as `T`,`reports` as `R` where `T`.`topic_id` = '".$topic_id."' and `T`.`report_id` = `R`.`report_id` limit 1");

if ($topic && $topic['user_id'] == $user->getUserId())
{
	$db->sendQuery("delete from `topics` where `topic_id` = '$topic_id'"); // удаляем запись
	$reports = 0;
	$reports += $db->getCell("select count(`friend_id`) from `friends` where `report_id` = '".$topic['report_id']."'");
	$reports += $db->getCell("select count(`comment_id`) from `comments` where `report_id` = '".$topic['report_id']."'");
	$reports += $db->getCell("select count(`topic_id`) from `topics` where `report_id` = '".$topic['report_id']."'");
	if ($reports == 0) // если больше не осталось записей из отчета report_id, удаляем отчет
		$db->sendQuery("delete from `reports` where `report_id` = '".$topic['report_id']."'");
}

goToPage("/coordinator/reports/look/topics");
?>